/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.hazard.alarm.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.hazard.alarm.dto.HazAlarmBrokenDTO;
import com.hopes.bizz.hazard.alarm.dto.HazAlarmBrokenExcelDTO;
import com.hopes.bizz.hazard.alarm.entity.HazAlarmBrokenEntity;
import com.hopes.bizz.hazard.alarm.entity.HazAlarmHandleRecordEntity;
import com.hopes.bizz.hazard.alarm.entity.HazAlarmOverproofEntity;
import com.hopes.bizz.hazard.alarm.service.HazAlarmBrokenService;
import com.hopes.bizz.hazard.alarm.service.HazAlarmHandleRecordService;
import com.hopes.bizz.hazard.alarm.vo.HazAlarmBrokenExcelVO;
import com.hopes.bizz.hazard.alarm.vo.HazAlarmBrokenVO;
import com.hopes.bizz.hazard.common.constant.HazardDicConstant;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.core.util.StringUtils;
import com.hopes.boss.common.excel.annotation.RequestExcel;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import com.hopes.boss.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 危险源断网报警
 *
 * @author daijie
 * @date 2023-11-16 19:21:03
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/alarmBroken")
@Tag(description = "alarmBroken", name = "危险源断网报警管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class HazAlarmBrokenController {

    private final HazAlarmBrokenService hazAlarmBrokenService;
    private final HazAlarmHandleRecordService hazAlarmHandleRecordService;

    /**
     * 分页查询
     *
     * @param page           分页对象
     * @param hazAlarmBroken 危险源断网报警
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('haz_alarmBroken_view')")
    public R getHazAlarmBrokenPage(Page page, HazAlarmBrokenDTO hazAlarmBroken) {
        final LambdaQueryWrapper<HazAlarmBrokenEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjUtil.isNotNull(hazAlarmBroken.getOrgId()), HazAlarmBrokenEntity::getOrgId, hazAlarmBroken.getOrgId());
        wrapper.eq(ObjUtil.isNotNull(hazAlarmBroken.getHazardSourceId()), HazAlarmBrokenEntity::getHazardSourceId, hazAlarmBroken.getHazardSourceId());
        wrapper.eq(StrUtil.isNotBlank(hazAlarmBroken.getAlarmStatus()), HazAlarmBrokenEntity::getAlarmStatus, hazAlarmBroken.getAlarmStatus());
        wrapper.eq(StrUtil.isNotBlank(hazAlarmBroken.getHandleStatus()), HazAlarmBrokenEntity::getHandleStatus, hazAlarmBroken.getHandleStatus());
        wrapper.ge(ObjUtil.isNotNull(hazAlarmBroken.getStartTime()), HazAlarmBrokenEntity::getAlarmTime, hazAlarmBroken.getStartTime());
        wrapper.le(ObjUtil.isNotNull(hazAlarmBroken.getEndTime()), HazAlarmBrokenEntity::getAlarmTime, hazAlarmBroken.getEndTime());
        wrapper.orderByDesc(HazAlarmBrokenEntity::getAlarmTime);
        return R.ok(hazAlarmBrokenService.page(page, wrapper));
    }


    /**
     * 通过id查询危险源断网报警
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('haz_alarmBroken_view')")
    public R getById(@PathVariable("id") Long id) {
        HazAlarmBrokenEntity entity = hazAlarmBrokenService.getById(id);
        HazAlarmBrokenVO vo = new HazAlarmBrokenVO();
        BeanUtils.copyProperties(entity, vo);

        LambdaQueryWrapper<HazAlarmHandleRecordEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(HazAlarmHandleRecordEntity::getModuleCode, HazardDicConstant.HAZ_ALARM_MODULE_BROKEN);
        wrapper.eq(HazAlarmHandleRecordEntity::getModuleId, id);
        wrapper.orderByDesc(HazAlarmHandleRecordEntity::getHandleEndTime);
        List<HazAlarmHandleRecordEntity> list = hazAlarmHandleRecordService.list(wrapper);
        vo.setHandleRecordList(list);
        return R.ok(vo);
    }

    /**
     * 新增危险源断网报警
     *
     * @param hazAlarmBroken 危险源断网报警
     * @return R
     */
    @Operation(summary = "新增危险源断网报警", description = "新增危险源断网报警")
    @SysLog("新增危险源断网报警")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('haz_alarmBroken_add')")
    public R save(@RequestBody HazAlarmBrokenEntity hazAlarmBroken) {
        return R.ok(hazAlarmBrokenService.save(hazAlarmBroken));
    }

    /**
     * 修改危险源断网报警
     *
     * @param hazAlarmBroken 危险源断网报警
     * @return R
     */
    @Operation(summary = "修改危险源断网报警", description = "修改危险源断网报警")
    @SysLog("修改危险源断网报警")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('haz_alarmBroken_edit')")
    public R updateById(@RequestBody HazAlarmBrokenEntity hazAlarmBroken) {
        return R.ok(hazAlarmBrokenService.updateById(hazAlarmBroken));
    }

    /**
     * 通过id删除危险源断网报警
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除危险源断网报警", description = "通过id删除危险源断网报警")
    @SysLog("通过id删除危险源断网报警")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('haz_alarmBroken_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(hazAlarmBrokenService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * excel模板
     *
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/template")
    @PreAuthorize("@pms.hasPermission('haz_alarmBroken_export')")
    public List<HazAlarmBrokenExcelVO> template() {
        return hazAlarmBrokenService.exportExcel(Arrays.asList(new HazAlarmBrokenEntity()));
    }

    /**
     * 导出excel 表格
     *
     * @param hazAlarmBrokenDTO 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('haz_alarmBroken_export')")
    public List<HazAlarmBrokenExcelVO> export(HazAlarmBrokenDTO hazAlarmBrokenDTO) {
        final LambdaQueryWrapper<HazAlarmBrokenEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjUtil.isNotNull(hazAlarmBrokenDTO.getOrgId()), HazAlarmBrokenEntity::getOrgId, hazAlarmBrokenDTO.getOrgId());
        wrapper.eq(ObjUtil.isNotNull(hazAlarmBrokenDTO.getHazardSourceId()), HazAlarmBrokenEntity::getHazardSourceId, hazAlarmBrokenDTO.getHazardSourceId());
        wrapper.eq(StrUtil.isNotBlank(hazAlarmBrokenDTO.getAlarmStatus()), HazAlarmBrokenEntity::getAlarmStatus, hazAlarmBrokenDTO.getAlarmStatus());
        wrapper.eq(StrUtil.isNotBlank(hazAlarmBrokenDTO.getHandleStatus()), HazAlarmBrokenEntity::getHandleStatus, hazAlarmBrokenDTO.getHandleStatus());
        wrapper.ge(ObjUtil.isNotNull(hazAlarmBrokenDTO.getStartTime()), HazAlarmBrokenEntity::getAlarmTime, hazAlarmBrokenDTO.getStartTime());
        wrapper.le(ObjUtil.isNotNull(hazAlarmBrokenDTO.getEndTime()), HazAlarmBrokenEntity::getAlarmTime, hazAlarmBrokenDTO.getEndTime());
        wrapper.orderByDesc(HazAlarmBrokenEntity::getAlarmTime);
        return hazAlarmBrokenService.exportExcel(hazAlarmBrokenService.list(wrapper));
    }

    /**
     * 导入excel 表格
     *
     * @param hazAlarmBrokenExcelDtoList 查询条件
     * @param bindingResult              错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('haz_alarmBroken_export')")
    public R importExcel(@RequestExcel List<HazAlarmBrokenExcelDTO> hazAlarmBrokenExcelDtoList, BindingResult bindingResult) {
        return hazAlarmBrokenService.importExcel(hazAlarmBrokenExcelDtoList, bindingResult);
    }
}
